package com.jn.glide.views;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.provider.MediaStore;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.jn.glide.R;
import com.jn.glide.adapter.AvatarListAdapter;
import com.jn.glide.adapter.AvatarResultAdapter;
import com.jn.glide.base.BaseActivity;
import com.jn.glide.contract.AvatarContract;
import com.jn.glide.entity.AvatarEntity;
import com.jn.glide.presenter.AvatarPresenter;

import java.util.ArrayList;
import java.util.List;

public class AvatarListActivity extends BaseActivity implements AvatarListAdapter.OnItemClickListener {
    private RecyclerView mRecyclerView;
    private ArrayList<AvatarEntity.ResultDTO.ListDTO> mList;
    private AvatarListAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_avatar;
    }

    @Override
    public void init() {
        Intent intent = getIntent();
        mList = intent.getParcelableArrayListExtra("list");
        mRecyclerView = findViewById(R.id.recycler);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new AvatarListAdapter(mList);
        mAdapter.addItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
        }
        showToast("点击可以将头像保存到相册");
    }

    @Override
    public void onItemClick(String url) {
        Glide.with(this)
                .asBitmap()
                .load(url)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        saveImageToGallery(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }

    public void saveImageToGallery(Bitmap bitmap) {
        String savedImageURL = MediaStore.Images.Media.insertImage(
                getContentResolver(),
                bitmap,
                "avatar",
                "avatar save"
        );
        showToast("保存成功! URL:" + savedImageURL);
    }
}
